package com.sxpi.chapter03;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class Test23 {
    public static void main(String[] args) throws CloneNotSupportedException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {

        // 获取单例对象
        HungrySingleton hungrySingleton = HungrySingleton.getInstance();
        System.out.println(hungrySingleton);


        HungrySingleton clone = (HungrySingleton) hungrySingleton.clone();
        System.out.println(clone);

        // 反射调用克隆方法
        Method method = hungrySingleton.getClass().getDeclaredMethod("clone");
        // 允许调用私有成员
        method.setAccessible(true);
        HungrySingleton cloneHungrySingleton = (HungrySingleton) method.invoke(hungrySingleton);
        System.out.println(cloneHungrySingleton);
    }
}